package com.example.essayjoke.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.ContentView;
import androidx.annotation.Nullable;

import com.example.essayjoke.R;

public class QQStepView extends View {
    private int mOuterColor = Color.RED;
    private int mInnerColor = Color.BLUE;
    // 6px
    private int mBorderWidth = 6;
    private int mStepTextColor = Color.BLACK;
    // 16px
    private int mStepTextSize = 16;
    private Paint mOuterPaint;
    private Paint mInnerPaint;
    private Paint mTextPaint;
    private RectF rectF;

    private int mStepMax;
    private int mCurrentStep;

    public QQStepView(Context context) {
        this(context, null);
    }

    public QQStepView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public QQStepView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.QQStepView);
        mOuterColor = array.getColor(R.styleable.QQStepView_outerColor, mOuterColor);
        mInnerColor = array.getColor(R.styleable.QQStepView_innerColor, mInnerColor);
        mBorderWidth = (int) array.getDimension(R.styleable.QQStepView_borderWidth, mBorderWidth);
        mStepTextColor = array.getColor(R.styleable.QQStepView_stepTextColor, mStepTextColor);
        mStepTextSize = array.getDimensionPixelSize(R.styleable.QQStepView_stepTextSize, mStepTextSize);

        mOuterPaint = new Paint();
        mOuterPaint.setAntiAlias(true);
        mOuterPaint.setStrokeWidth(mBorderWidth);
        mOuterPaint.setColor(mOuterColor);
        mOuterPaint.setStyle(Paint.Style.STROKE);
        // 设置圆角
        mOuterPaint.setStrokeCap(Paint.Cap.ROUND);

        mInnerPaint = new Paint();
        mInnerPaint.setAntiAlias(true);
        mInnerPaint.setStrokeWidth(mBorderWidth);
        mInnerPaint.setColor(mInnerColor);
        mInnerPaint.setStyle(Paint.Style.STROKE);
        // 设置圆角
        mInnerPaint.setStrokeCap(Paint.Cap.ROUND);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(mStepTextColor);
        mTextPaint.setTextSize(mStepTextSize);

        rectF = new RectF(0, 0, getWidth(), getHeight());
        array.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // todo ?
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 调用者在布局文件中 宽高的设置

        // 宽高不一致，取最小值，因为这个我们实现是正方形的

        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int min = Math.min(width, height);
        setMeasuredDimension(min, min);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        // View的onDraw是空实现，可以删除
        //super.onDraw(canvas);
        // 1.画外圆弧

        int center = getWidth() / 2;
        int radius = getWidth() / 2 - mBorderWidth / 2;
        rectF = new RectF(center-radius, center-radius, center+radius, center+radius);
        canvas.drawArc(rectF, 135, 270, false, mOuterPaint);
        // 2.画内圆弧
        if (mStepMax == 0) {
            return;
        }
        float sweepAngle = (float) mCurrentStep / mStepMax;
        canvas.drawArc(rectF, 135, 270 * sweepAngle, false, mInnerPaint);
        // 3.画文字
        String text = mCurrentStep+"";
        // 3.1获取文字的宽高
        Rect textRect = new Rect();
        mTextPaint.getTextBounds(text, 0, text.length(), textRect);
        int dx = getWidth()/2 - textRect.width()/2;
        // 3.2基线
        Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt();
        int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 -fontMetricsInt.bottom;
        int baseLine = getHeight() / 2 + dy;
        canvas.drawText(text, dx, baseLine, mTextPaint);
    }

    public synchronized void setStepMax(int mStepMax) {
        this.mStepMax = mStepMax;
    }

    public synchronized void setCurrentStep(int mCurrentStep) {
        this.mCurrentStep = mCurrentStep;
        invalidate();
    }
}
